#!/usr/bin/python
import os


def touch(file_name):
    if file_name in os.listdir('.'):
        print("file exist!")
    else:
        print("creating %s" % file_name)
        fid = open(file_name, 'w')
        fid.close()


path = "/Users/liuweitao/Documents/"
date = "20241222"
logPath = path + date + "/"
errorFile = path + "error_" + date + ".log"
print(errorFile)
files = os.listdir(logPath)
if not os.path.exists(errorFile):
    touch(errorFile)
s = []
logErrorFile = open(errorFile, "w")
noCache = "缓存内无该班级数据,请先推送班级基本信息"
syncError = "同步错误"
verifyPhoto = "上海监管上报实名认证照片"
executeFail = "执行失败"
userExcept="用户上报信息异常"
gateWay = "<html>"
try:
    for file in files:
        if not os.path.isdir(file):
            if file == '.DS_Store':
                continue
            f = open(logPath + "/" + file)
            iter_f = iter(f)
            for line in iter_f:
                if line.find(syncError) != -1 or line.find(noCache) != -1 or line.find(gateWay) != -1 or line.find(executeFail) != -1 or line.find(userExcept) != -1:
                    logErrorFile.write(line)
            f.close()
    logErrorFile.close()
    print(date + "分析日志文件数:" + str(len(files)))
finally:
    logErrorFile.close()
